﻿using System.Collections.Generic;
using System.Web;

using EHTSaleManager.Code.Entity;
using EHTSaleManager.Code.BLL;
/**
 * The ShoppingCart class
 * 
 * Holds the items that are in the cart and provides methods for their manipulation
 */
namespace EHTSaleManager.Code.Entity
{
    public class ShoppingCart
    {
        #region Properties

        public Order theOrder { get; set; }
        public List<CartItem> Items { get; private set; }

        #endregion

        #region Singleton Implementation

        // Readonly properties can only be set in initialization or in a constructor

        public static ShoppingCart Instance
        {
            get
            {
                ShoppingCart c = null;
                if (HttpContext.Current.Session["ASPNETShoppingCart"] == null)
                {
                    c = new ShoppingCart();
                    c.Items = new List<CartItem>();
                    HttpContext.Current.Session.Add("ASPNETShoppingCart", c);
                }
                else
                {
                    c = (ShoppingCart)HttpContext.Current.Session["ASPNETShoppingCart"];
                }
                return c;
            }
        }
      
        // A protected constructor ensures that an object can't be created from outside
        protected ShoppingCart() { }

        #endregion

        #region Item Modification Methods
        /**
	 * AddItem() - Adds an item to the shopping 
	 */
        public void AddItem(int productId, int quantity)
        {
            // Create a new item to add to the cart
            CartItem newItem = new CartItem(productId);

            // If this item already exists in our list of items, increase the quantity
            // Otherwise, add the new item to the list
            if (Items.Contains(newItem))
            {
                foreach (CartItem item in Items)
                {
                    if (item.Equals(newItem))
                    {
                        item.Quantity++;
                        return;
                    }
                }
            }
            else
            {
                newItem.Quantity = quantity;
                Items.Add(newItem);
            }
        }

        /**
         * SetItemQuantity() - Changes the quantity of an item in the cart
         */
        public void SetItemQuantity(int productId, float quantity)
        {
            // If we are setting the quantity to 0, remove the item entirely
            if (quantity == 0)
            {
                RemoveItem(productId);
                return;
            }

            // Find the item and update the quantity
            CartItem updatedItem = new CartItem(productId);

            foreach (CartItem item in Items)
            {
                if (item.Equals(updatedItem))
                {
                    item.Quantity = quantity;
                    return;
                }
            }
        }

        /**
         * RemoveItem() - Removes an item from the shopping cart
         */
        public void RemoveItem(int productId)
        {
            CartItem removedItem = new CartItem(productId);
            Items.Remove(removedItem);
        }
        #endregion

        /**
         * RemoveSession() - Removes session for shopping cart
         */
        public void RemoveShoppingCart()
        {
            HttpContext.Current.Session["ASPNETShoppingCart"] = null;
        }

        #region Reporting Methods
        /**
	 * GetSubTotal() - returns the total price of all of the items
	 *                 before tax, shipping, etc.
	 */
        public float GetSubTotal()
        {
            float subTotal = 0;
            foreach (CartItem item in Items)
                subTotal += item.TotalPrice;

            return subTotal;
        }
        #endregion

        #region Save into Database
        public void SaveDataBase()
        {
            if (theOrder == null)
                return;
            //Insert Order
            int orderID = OrderProvider.UIOrder(theOrder, true);
            //Insert OrderDetail
            OrderDetail theOrderDetail;
            int totalItem = Items.Count;

            for (int i = 0; i < totalItem; ++i)
            {
                theOrderDetail = new OrderDetail();
                theOrderDetail.Discount = 0;
                theOrderDetail.OrderID = orderID;
                theOrderDetail.ProductID = Items[i].ProductId;
                theOrderDetail.Quantity = Items[i].Quantity;
                theOrderDetail.UnitPrice = Items[i].UnitPrice;
                OrderDetaiProvider.UIOrderDetal(theOrderDetail, true);
                theOrderDetail = null;
            }
        }
        #endregion
    }
}
